module.exports = (app, router) => {
    const article = require("../../models/article");
    const articleType = require('../../models/articleType')
    const count = require('../../models/counts')
    const {
        requestResult,
        pagingResult
    } = require('../../plugins/index')
    const moment = require('moment')
    //发布文章
    router.post('/addArticle', async (req, res) => {
        //文章id自增 1
        const artileId = await count.findOneAndUpdate({
            name: 'acticleId'
        }, {
            $inc: {
                'id': 1
            }
        }, {
            new: true
        })
        let time = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss')
        req.body['updateTime'] = time
        if (artileId) {
            req.body.id = artileId.id
            req.body['createTime'] = req.body['createTime'] || time
        } else {
            //第一次发文章
            const info = {
                name: 'acticleId',
                id: 1000
            }
            const i = await count.create(info)
            req.body.id = i.id
        }
        const result = await article.create(req.body)
        if (result) {
            let val = await articleType.findByIdAndUpdate(req.body.articleType, {
                $inc: {
                    'nums': 1
                }
            }, {
                new: true
            })
        }
        res.send(requestResult(result))
    })
    //查看文章列表
    router.post('/findArticle', async (req, res) => {
        const page = Number(req.body.page) || 1
        const size = Number(req.body.size) || 10
        let db;
        db = req.body.articleType ? article.find({
            'articleType': req.body.articleType
        }).populate('articleType') : article.find().populate('articleType')
        let data = await pagingResult(db, page, size)
        res.send(requestResult(data))
        // let data = await pagingResult(db, page, size)
        // res.send(requestResult(data))
        // Promise.all([
        //     article.countDocuments(),
        //     article.find().sort({ 'time': -1 }).limit(size).skip((page - 1) * size)
        // ]).then((item) => {
        //     const data = {
        //         total: item[0] || 0,
        //         data: item[1] || null,
        //         page: page,
        //         size: size
        //     }
        //     res.send(requestResult(data))
        // })
    })
    //根据文章id查询文章详情
    router.get('/findArticle/:id', async (req, res) => {
        let data = await article.findOne({
            id: req.params.id
        }).populate('articleType')
        res.send(requestResult(data))
    })
    //编辑文章
    router.post('/editArticle', async (req, res) => {
        let time = moment(Date.now()).format('YYYY-MM-DD HH:mm:ss')
        req.body['updateTime'] = time
        let data = await article.findOneAndUpdate({
            'id': req.body.id
        }, req.body, (err, item) => {
            res.send(requestResult(item))
        })
    })
    //删除文章
    router.delete('/removeArticle/:id', async (req, res) => {
        await article.findOneAndRemove({
            'id': req.params.id
        }, async (err, item) => {
            let val = await articleType.findByIdAndUpdate(req.body.articleType, {
                $inc: {
                    'nums': -1
                }
            }, {
                new: true
            })
            res.send(requestResult(item))
        })

    })
    app.use('/admin/api', router)
}